<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class User extends CI_Controller {

	

	private $callbackReturn;


	public function index()
	{
		echo "<h1>Home Goes Here</h1>";
		echo "<a href='".base_url()."user/login'>[Log In]</a>";
		echo "<br />";
	}

	public function login()
	{
		$this->load->view('login');
	}

	public function signup()
	{
		 $this->load->view('signup');
	}
        
        public function intro()
        {
           $this->load->view('includes/intro');
        }


        public function dashboard()
	{
          $this->load->library('SESSION_HANDLER');
          $allow = $this->session_handler->check_session(array('id','is_logged_in'));
           
	  if($allow)
	  {
                  $this->load->model('model_users');
                  $this->load->model('model_posts');
                  $this->load->model('model_groups');
                  
                  # GET GROUP ID WHERE USER BELONGS TO
                  $group_id    = $this->model_users->get_user_group($_SESSION['id']);
                  
                  # GET ALL GROUP POSTS
                  $posts = $this->model_posts->get_group_post($group_id);
                  
                  
		  $userInfo         = $this->get_user_info(array('member_id'=>$_SESSION['id']));
                  $content['posts'] = $posts; 
                  
                  $group['members'] = $this->model_groups->get_group_members($group_id);
                  $group['info']    = $this->model_groups->get_group_info($group_id);
                  
                  
                  
                  # BEGIN LOADING THE CONTENT -
                  $this->load->view('includes/header');
                 
                  # CREATE DASHBOARD STRUCTURE
                  $this->load->view('user_header',$userInfo);
                  echo'<div id="mainContainer" style="margin-left:auto; margin-right:auto; margin-top:10px; width: 800px;">';
                    $this->load->view('includes/navigation');

                    $this->load->view('timeline',$content);
                    $this->load->view('includes/right_control',$group);

                  echo'</div>';
                  $this->load->view('includes/footer');
	  }

	  else
	  {  
              #  REDIRECT THE USER TO THE RESTRICTED PAGE OR MAYBE JUST REDIRECT HIM TO LOGIN IN THE FUTURE 
              redirect('user/restricted');
	  } 
	}	

	

	public function restricted()
	{
	 $this->load->view('restricted');
	}

		

	public function validate()
	{
	 $this->load->library('form_validation');
	 $this->form_validation->set_rules('txtEmail','Email','required|trim|xss_clean|callback_validate_account');
	 $this->form_validation->set_rules('txtPassword','Password','required|md5|trim');

		if($this->form_validation->run())
		{
			
                        $this->load->library('SESSION_HANDLER');
                        
                        $data = array('id'=>  $this->callbackReturn , 'is_logged_in'=>1);
                        $this->session_handler->create_sessions($data);
                        redirect('user/dashboard');	
		}
			else $this->load->view('login');
        }	

	

	public function validate_account()
	{
                $this->load->model('model_users');
             
                $id = $this->model_users->allow_log_in();
		if($id)
		{
			$this->callbackReturn = $id; 
			return true; 
		}
		else
		{
			$this->form_validation->set_message('validate_account','Access Denied');
			return false; 	  
		}	 
	}

	

	public function register_validation()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('txtFirstName','First Name','required|trim|ucfirst');
		$this->form_validation->set_rules('txtLastName','Last Name','required|trim|ucfirst');
		$this->form_validation->set_rules('txtEmail','Email','required|trim|valid_email|is_unique[member.email]');
		$this->form_validation->set_rules('txtPassword','Password','required|trim');
		$this->form_validation->set_rules('txtPassword2','Confirm Password','required|trim|matches[txtPassword]');
		$this->form_validation->set_message('is_unique',$this->input->post('txtEmail').' is already taken');
		$this->form_validation->set_message('matches','Passwords does not match');

		if($this->form_validation->run())
		{
			# ADD USER TO DB 
			$this->load->model('model_users');
                        $id = $this->model_users->add_user();
			if($id)
			{
			        $this->load->library('SESSION_HANDLER');
                                	
                                $data = array('is_logged_in' => 1,'id'=>$id);	
				$this->session_handler->create_sessions($data);
				redirect('user/dashboard');
			}	
			else
			{
				echo "COULD NOT ADD TO DB";	
			} 	
		}
		else
		{
			$this->load->view('signup');
		}
	}

	

	public function get_user_info($conditions = array())
	{
		$this->load->model('model_users');
		$result = $this->model_users->get_user_info($conditions);
	        return $result;
	}

	public function logout()
	{
                session_start();
                session_destroy();
                
                redirect('user/login');
	}

} //  END OF CLASS USER



